﻿/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Material 2.0
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.12
import QtGraphicalEffects 1.0
import Qt.labs.platform 1.0
import "qrc:/MaterialUI/"
import "qrc:/MaterialUI/Interface/"
Item {
    property string selectimage: "user_default"

    Component.onCompleted: {
        selectimage=ChatManage.MyInfo["user_img"]
        imagemodel.clear()
        imagemodel.append({"image_name":"user_default"})
        for(var i=1;i<27;++i){
            imagemodel.append({"image_name":"user_"+i})
        }
    }

    function initOpen(){
        userconfig.initOpen()
    }

    function saveNowConfig(){
        userconfig.saveNowConfig()
    }

    ListModel{
        id:imagemodel
    }

    MouseArea{
        anchors.fill: parent
        onWheel: {

        }
    }

    ListModel{
        id:itemmodel
        ListElement{name:"账号设置"}
        ListElement{name:"头像设置"}
        ListElement{name:"文件管理"}
        ListElement{name:"关于Chat"}
    }

    Rectangle{
        id:split
        anchors{
            left: listView.right
            leftMargin: 8
            top: listView.top
            bottom: parent.bottom
        }
        width: 2
        color: "#F5F5F5"
    }
    ListView{
        id: listView
        width: 80
        anchors{
            left: parent.left
            top: parent.top
            topMargin: 20
            bottom: parent.bottom
        }
        model: itemmodel
        currentIndex:0
        delegate: ItemDelegate {
            width: listView.width
            height: 40
            Rectangle{
                id:textShow
                anchors.fill: parent
                color: "#00000000"
                MaterialLabel{
                    text: name
                    font.pixelSize: 15
                    anchors.centerIn: parent
                    color: listView.currentIndex==index?"#8BC34A":"black"
                }
            }
            Rectangle{
                anchors{
                    left: textShow.right
                    leftMargin: 8
                    top: textShow.top
                    topMargin: 7
                    bottom:textShow.bottom
                    bottomMargin: 7
                }
                width: 2
                color: listView.currentIndex==index?"#8BC34A":"#F5F5F5"
            }

            MouseArea{
                anchors.fill: parent
                onClicked: {
                    if(listView.currentIndex==index)
                        return
                    listView.currentIndex = index
                }
            }
        }
    }


    SwipeView {
        id: view
        currentIndex: listView.currentIndex
        anchors{
            left: listView.right
            leftMargin: 10
            top: parent.top
            bottom: parent.bottom
            right: parent.right
        }
        clip: true
        interactive:false
        orientation:Qt.Vertical
        Item {
            id: firstPage
            UserConfig{
                id:userconfig
            }
        }
        Item {
            id: secondPage
            Rectangle{
                id: image
                width: 90
                height: 90
                anchors.left: parent.left
                anchors.top: parent.top
                anchors.leftMargin: 10
                anchors.topMargin: 20
                Image {
                    anchors.fill: parent
                    anchors.margins: 5
                    source: "qrc:/"+selectimage+".png"
                    fillMode: Image.PreserveAspectFit
                    layer.enabled: true
                    layer.effect:DropShadow {
                        horizontalOffset: 0
                        verticalOffset: 0
                        radius: 4
                        samples: 9
                        color: "#000000"
                        spread:0
                    }
                }
                layer.enabled: true
                layer.effect: DropShadow {
                    horizontalOffset: 0
                    verticalOffset: 0
                    radius: 4
                    samples: 9
                    color: "#000000"
                    spread: 0
                }
                Rectangle{
                    id:showTip
                    anchors.fill: parent
                    visible: false
                    opacity:0.5
                    MaterialLabel{
                        font.pixelSize: 15
                        text: "还 原"
                        anchors.fill: parent
                        horizontalAlignment: Label.AlignHCenter
                        verticalAlignment: Label.AlignVCenter
                    }
                }
                MouseArea{
                    anchors.fill: parent
                    hoverEnabled: enabled
                    onEntered: {
                        showTip.visible = true
                    }
                    onExited: {
                        showTip.visible = false
                    }
                    onClicked: {
                        selectimage=ChatManage.MyInfo["user_img"]
                    }
                }
            }

            Rectangle{
                id:imageView
                anchors{
                    left:image.right
                    top:image.top
                    bottom: parent.bottom
                    right: parent.right
                    leftMargin: 20
                    rightMargin: 10
                    bottomMargin: 10
                }

                Component {
                    id: contactDelegate
                    Item {
                        width: grid.cellWidth; height: grid.cellHeight
                        Column {
                            anchors.fill: parent
                            Rectangle{
                                id:baccolor
                                width: 85
                                height: 85
                                radius: 3
                                color: "#00000000"
                                Image {
                                    id:imageItem
                                    source: "qrc:/"+image_name+".png"
                                    anchors.fill: parent
                                    anchors.margins: 5
                                    anchors.horizontalCenter: parent.horizontalCenter
                                    fillMode: Image.PreserveAspectFit
                                    layer.enabled: true
                                    layer.effect:DropShadow {
                                        horizontalOffset: 0
                                        verticalOffset: 0
                                        radius: 4
                                        samples: 9
                                        color: "#000000"
                                        spread:0
                                    }
                                    PropertyAnimation {
                                        id: animation
                                        target: imageItem
                                        property: "scale"
                                        to: 1;
                                        duration: 150
                                    }
                                    MouseArea{
                                        anchors.fill: parent
                                        hoverEnabled: true
                                        onEntered: {
                                            baccolor.color = "lightskyblue"
                                            imageItem.scale = 0.9
                                            animation.running = true
                                        }
                                        onExited: {
                                            baccolor.color = "#00000000"
                                        }

                                        onClicked: {
                                            selectimage = image_name
                                        }
                                    }
                                }
                            }
                        }


                    }
                }
                GridView{
                    id:grid
                    anchors.fill: parent
                    model: imagemodel
                    cellWidth:100
                    cellHeight:100
                    focus: true
                    clip: true
                    delegate:contactDelegate
                    ScrollBar.vertical: ScrollBar {}
                }
            }

        }
        Item{
            id:thirdPage

                MaterialLabel{
                    id:path
                    anchors{
                        left: parent.left
                        leftMargin: 20
                        top: parent.top
                        topMargin: 50
                    }
                    font.pixelSize: 15
                    width: 80
                    height: 50
                    text: "保存路径:"
                    horizontalAlignment: Label.AlignRight
                    verticalAlignment: Label.AlignTop
                }
                TextEdit {
                    id:user_path
                    anchors{
                        left: path.right
                        leftMargin: 15
                        right: parent.right
                        top: path.top
                        topMargin: 3
                        bottom: path.bottom
                    }
                    readOnly: true
                    wrapMode: TextArea.Wrap
                    selectByMouse: true
                    font.pixelSize: 15
                    text: ChatManage.MyConfig["SoftWare/TempPath"].substring(8)
                    Layout.fillWidth: true
                    horizontalAlignment: Label.AlignLeft
                    verticalAlignment: Label.AlignTop
                }

            MaterialButton{
                id:changepath
                width: 90
                height: 28
                anchors.top: openfile.top
                anchors.right: openfile.left
                anchors.rightMargin: 40
                text: "更 改"
                onClicked: {
                    folderDialog.open()
                }
            }
            MaterialButton{
                id:openfile
                width: 90
                height: 28
                anchors.right: parent.right
                anchors.rightMargin: 20
                anchors.top: user_path.bottom
                anchors.topMargin: 20
                text: "打开文件夹"
                onClicked: {
                    ChatManage.openTempFile(ChatManage.MyConfig["SoftWare/TempPath"]+"/")
                }
            }
        }
        Item{
            id:fourPage
            ColumnLayout {
                id:columnlayout
                anchors.fill: parent
                Rectangle{
                    width: 90
                    height: 90
                    Layout.alignment: Qt.AlignCenter
                    Image {
                        anchors.fill: parent
                        anchors.margins: 5
                        source: "qrc:/user_kong.png"
                        fillMode: Image.PreserveAspectFit
                        layer.enabled: true
                        layer.effect:DropShadow {
                            horizontalOffset: 0
                            verticalOffset: 0
                            radius: 4
                            samples: 9
                            color: "#000000"
                            spread:0
                        }
                    }
                    layer.enabled: true
                    layer.effect: DropShadow {
                        horizontalOffset: 0
                        verticalOffset: 0
                        radius: 4
                        samples: 9
                        color: "#000000"
                        spread: 0
                    }
                }

                MaterialLabel{
                    font.pixelSize: 16
                    text: "作者: kvell"
                    Layout.alignment: Qt.AlignCenter
//                    horizontalAlignment: Label.AlignRight
//                    verticalAlignment: Label.AlignVCenter
                }
                MaterialLabel{
                    font.pixelSize: 16
                    text: "版本: 1.0.0"
                    Layout.alignment: Qt.AlignCenter
                }
                MaterialLabel{
                    font.pixelSize: 16
                    Layout.alignment: Qt.AlignCenter
                    text: "邮箱: kong_w@foxmail.com"
                }
            }
        }
    }

    FolderDialog {
           id: folderDialog
           options:FolderDialog.ShowDirsOnly
           currentFolder:ChatManage.MyConfig["SoftWare/TempPath"]
           onAccepted: {
               ChatManage.writeConfigValue("SoftWare/TempPath",folder)
           }
       }

//    PageIndicator {
//        id: indicator

//        count: view.count
//        currentIndex: view.currentIndex

//        anchors.bottom: view.bottom
//        anchors.horizontalCenter: parent.horizontalCenter
//    }
}
